package com.dfe.backend.common.config;

import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author LiChang(lclicht @ qq.com)
 * @version 1.0
 * @class ApplicationContextConfig
 * @since 2023-12-6 10:13
 */
@Component
public class ApplicationContextConfig implements ApplicationContextAware {

    @Getter
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextConfig.applicationContext = applicationContext;
    }

    /**
     * 【描述】: 通过name获取 Bean
     *
     * @param beanName beanName
     * @return {@link Object}
     * @author LiChang
     * @since 2024-2-20 9:36
     */
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    /**
     * 【描述】: 通过class获取Bean
     *
     * @param clazz clazz
     * @return {@link T}
     * @author LiChang
     * @since 2024-2-20 9:36
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 【描述】: 通过name,以及Clazz返回指定的Bean
     *
     * @param name  name
     * @param clazz clazz
     * @return {@link T}
     * @author LiChang
     * @since 2024-2-20 9:36
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

